﻿
using System;
using SqlSugar;
using System.Threading.Tasks;
using System.Linq.Expressions;

using Oliving.Edge.Model;
using Oliving.Edge.Model.Dto;
using Oliving.Edge.Model.Models;
using Oliving.Edge.IRepository;
using Oliving.Edge.IRepository.UnitOfWork;
using Oliving.Edge.Repository.Base;

namespace Oliving.Edge.Repository
{
    /// <summary>
    /// DTSN_TongDaoRepository
    /// </summary>
    public class DTSN_TongDaoRepository : BaseRepository<DTSN_TongDao>, IDTSN_TongDaoRepository
    {
        public DTSN_TongDaoRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
        {
        }


        /// <summary>
        /// 自定义分页查询
        /// </summary>
        /// <param name="whereExpression">查询条件</param>
        /// <param name="intPageIndex" > 页码（下标0）</param>
        /// <param name="intPageSize" > 页大小</param>
        /// <param name="strOrderByFileds" > 排序字段，如name asc,age desc</param>
        /// <returns></returns>
        public async Task<PageModel<DTSN_TongDaoPageDto>> QueryPageList(
            Expression<Func<DTSN_TongDao, bool>> whereExpression,
            int intPageIndex = 1,
            int intPageSize = 20,
            string strOrderByFileds = null)
        {

            if (false)
            {
                #region 单表

                RefAsync<int> totalCount = 0;
                var list = await Db.Queryable<DTSN_TongDao>()
                 .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
                 .WhereIF(whereExpression != null, whereExpression)
                 .Select<DTSN_TongDaoPageDto>()
                 .ToPageListAsync(intPageIndex, intPageSize, totalCount);

                int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).ObjToInt();
                return new PageModel<DTSN_TongDaoPageDto>()
                {
                    dataCount = totalCount,
                    pageCount = pageCount,
                    page = intPageIndex,
                    PageSize = intPageSize,
                    data = list
                };

                #endregion
            }

            {
                #region 多表
                {
                    RefAsync<int> totalCount = 0;
                    var list = await Db.Queryable<DTSN_TongDao, DTSN_ShangWeiJi>

                        ((Td, Swj) => new object[] { JoinType.Left, Td.TD_ShangWeiji == Swj.ID })

                     .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
                     .WhereIF(whereExpression != null, whereExpression)
                     .Select<DTSN_TongDaoPageDto>()
                     .ToPageListAsync(intPageIndex, intPageSize, totalCount);

                    int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).ObjToInt();
                    return new PageModel<DTSN_TongDaoPageDto>()
                    {
                        dataCount = totalCount,
                        pageCount = pageCount,
                        page = intPageIndex,
                        PageSize = intPageSize,
                        data = list
                    };
                }
                #endregion
            }
        }


    }
}

